package com.ihrm.employee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.domain.employee.EmployeeArchive;
import com.ihrm.domain.employee.EmployeeArchiveExample;
import com.ihrm.employee.mapper.EmployeeArchiveMapper;
import com.ihrm.employee.service.api.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Autowired
    private EmployeeArchiveMapper employeeArchiveMapper;

    @Autowired
    private IdWorker idWorker;

    public void save(EmployeeArchive archive) {
        archive.setId(idWorker.nextId() + "");
        archive.setCreateTime(new Date());
        employeeArchiveMapper.insertSelective(archive);
    }

    public EmployeeArchive findLast(String companyId, String month) {
        return employeeArchiveMapper.findLast(companyId, month);
    }

    public List<EmployeeArchive> findAll(Integer page, Integer pagesize, String year, String companyId) {
//        int index = (page - 1) * pagesize;
//        return archiveDao.findAllData(companyId, year + "%", index, pagesize);
        return null;
    }

    public Long countAll(String year, String companyId) {
//
        return null;
    }


    public PageInfo<EmployeeArchive> findSearch(Map<String, Object> map, int page, int size) {
        // 1.开启分页功能
        PageHelper.startPage(page, size);
        EmployeeArchiveExample employeeArchiveExample = new EmployeeArchiveExample();
        EmployeeArchiveExample.Criteria criteria = employeeArchiveExample.createCriteria();
        // 企业id
        if (map.get("companyId") != null && !"".equals(map.get("companyId"))) {
            criteria.andCompanyIdEqualTo((String) map.get("companyId"));
        }
        if (map.get("year") != null && !"".equals(map.get("year"))) {
            criteria.andMonthEqualTo((String) map.get("year"));
        }
        List<EmployeeArchive> employeeArchiveList = employeeArchiveMapper.selectByExample(employeeArchiveExample);
        // 3.为了方便页面使用将 userList 封装为
        PageInfo<EmployeeArchive> pageInfo = new PageInfo<>(employeeArchiveList);
        return pageInfo;
    }

    /**
     * 动态条件构建
     */
    public List<EmployeeArchive> createSpecification(Map searchMap) {
//        EmployeeArchiveExample employeeArchiveExample = new EmployeeArchiveExample();
//        EmployeeArchiveExample.Criteria criteria = employeeArchiveExample.createCriteria();
//        // 企业id
//        if (searchMap.get("companyId") != null && !"".equals(searchMap.get("companyId"))) {
//            criteria.andCompanyIdEqualTo((String) searchMap.get("companyId"));
//        }
//        if (searchMap.get("year") != null && !"".equals(searchMap.get("year"))) {
//            criteria.andMonthEqualTo((String) searchMap.get("year"));
//        }
//        return employeeArchiveMapper.selectByExample(employeeArchiveExample);
        return null;
    }
}
